import DList from "@/components/dlist";
import mealPopup from "./components/meal_popup.vue";
import yz_uploader from "components/ui_components/yz_uploader";
export default {
  data() {
    return {
      form: {
        category_name: "",
        content: "",
        title: "",
        is_headline: false,
        pic: "",
      },
      category_show: false,
      meal_show: false,
      type: "",
      id: "",
      headline_fee: 0,
      currentPackage: {},
      classifyData: [],
      order_id: "",
      fileList1: [],
      is_show: false
    };
  },
  mounted() {
    this.type = this.$route.params.type;
    this.id = this.$route.params.id;
    if(this.type == "check") {
      this.getNeedDetail();
    } else {
      this.getClassifyData();
    }
  },
  activated() {
    if(this.type == "check") {
      // this.getNeedDetail();
    } else {
      this.getEnterprisePer();
    }
  },
  methods: {
    // 企业-需求详情
    getNeedDetail() {
      $http.get("plugin.be-within-call.frontend.enterprise.need-detail", { id: this.id }).then(({ data , result , msg }) => {
        if (result) {
          this.fileList1.push(data.pic_url);
          this.form.category_name  =data.has_one_category.title;
          this.form.title = data.title;
          this.form.content = data.content;
          this.form.is_headline = data.is_headline == 1 ? true : false;
          this.headline_fee = data.headline_fee;
          this.currentPackage = data.has_one_package;
          this.order_id = data.order_id;
        } else {
          this.$toast(msg);
        }
      });
    },
    inputimg(imgVal) {
      this.form.pic = imgVal[0];
    },
    chooseCategory(id) {
      for(let item of this.classifyData) {
        if(id == item.id) {
          this.$set(item,'ishow',true)
        }else {
          this.$set(item,'ishow',false)
        }
      }
    },
    optionPackage(op) {
      this.currentPackage = op[0];
      this.meal_show = false;
    },
    sureOption() {
      this.form.category_name = this.classifyData.filter(item => item.ishow)[0].title;
      this.category_show = false;
    },
    // 获取分类
    getClassifyData() {
      $http.get("plugin.be-within-call.frontend.index.get-category-list", { sort_type: 3 }).then(({ data , result , msg }) => {
        if (result) {
          this.classifyData = data;
          // this.getEnterprisePer();
        } else {
          this.$toast(msg);
        }
      });
    },
    // 企业-发布需求前置接口
    getEnterprisePer() {
      $http.get("plugin.be-within-call.frontend.create-enterprise-need.pre", { }).then(({ data , result , msg }) => {
        if (result) {
          this.headline_fee = data.headline_fee;
          if(data.currentPackage) {
            this.currentPackage = data.currentPackage;
            this.is_show = true;
          }else {
            this.$router.push(
              this.fun.getUrl("anyEnterprisePackage", {})
            );
          }
        } else {
          this.$toast(msg);
        }
      });
    },
    submit() {
      if(this.type == "check") {
        this.$router.push(
          this.fun.getUrl("anyOrderDetail", {type: "enterprise",order_id:this.order_id})
        );
      }else {
        let form = {
          is_headline: this.form.is_headline ? 1 : 0,
          category_id: this.form.category_name ? this.classifyData.filter(item => item.ishow).length > 0 ? this.classifyData.filter(item => item.ishow)[0].id : "" : "",
          title: this.form.title,
          content: this.form.content,
          pic: this.form.pic ? this.form.pic : ""
        }
        $http.get("plugin.be-within-call.frontend.create-enterprise-need.index", {
          ...form
        }).then(({ data , result , msg }) => {
          if (result) {
            this.$router.push(
              this.fun.getUrl("orderpay", {
                status: 2,
                order_ids: data.order_ids
              })
            );
          } else {
            this.$toast(msg);
          }
        });
      }
    },
    goEnterprisePage() {
      this.$router.push(
        this.fun.getUrl("anyEnterprisePackage", {})
      );
    }
  },
  components: { DList , mealPopup , yz_uploader}
};
